<?php
/**
 * Created by PhpStorm.
 * User: Mysic
 * Date: 2020/10/21
 * Time: 13:46
 */

namespace app\common\mysic\user;


use app\common\model\User;
use app\common\model\user\BalanceLog;

use app\common\mysic\MysicBase;

class UserAccountOp extends MysicBase
{
    const WEIGUDONG = 1;
    const ORDINARY = 0;
    /**
     * @var User
     */
    protected $userEntity;
    protected $orderEntity;

    public function setUserEntity($userEntity)
    {
        $this->userEntity = $userEntity;
        return $this;
    }
    public function setOrderEntity($orderEntity)
    {
        $this->orderEntity = $orderEntity;
        return $this;
    }
    public function upgradeIdentity($identity)
    {
        $this->userEntity->shareholder = $identity;
        $this->userEntity->save();
    }
    public function upgradeBalance($balance, $change)
    {
        BalanceLog::add(50, [
            'user_id'  =>$this->userEntity->user_id,
            'money'    =>$change,
            'wxapp_id' =>$this->userEntity->wxapp_id,
            'remark' => $this->orderEntity->order_no,
        ], ['order_no' =>$this->orderEntity->order_no]);
        if(bccomp(strval($balance), strval($this->userEntity->balance), 2) == 0){
            return true;
        }
        $this->userEntity->balance = $balance;
        return $this->userEntity->isUpdate(true)->save();
    }


}